package com.zone.vehicle.superApps.view.login;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.lifecycle.ViewModelProvider;

import com.zone.vehicle.superApps.databinding.FragmentLoginBinding;
import com.zone.vehicle.superApps.model.LoginViewModel;
import com.zone.vehicle.superApps.model.ViewModelFactory;
import com.zone.vehicle.superApps.view.common.base.BaseFragment;
import com.zone.vehicle.superApps.view.common.listener.ActionEnum;
import com.zone.vehicle.superApps.view.common.listener.ActionListener;
import com.zone.vehicle.superApps.view.login.message.LoginMessageFragment;
import com.zone.vehicle.superApps.view.login.secret.LoginSecretFragment;
import com.zone.vehicle.superApps.view.login.setSecret.ResetSecretFragment;
import com.zone.vehicle.superApps.view.login.setSecret.SetSecretFragment;

import org.jetbrains.annotations.NotNull;

public class LoginFragment extends BaseFragment {

    private static final String TAG = LoginFragment.class.getSimpleName();

//    public static final String LOGIN_SECRET_FRAGMENT = LoginSecretFragment.class.getSimpleName();
    public static final String LOGIN_SECRET_FRAGMENT = "LoginSecretFragment";
//    public static final String LOGIN_MESSAGE_FRAGMENT = LoginMessageFragment.class.getSimpleName();
    public static final String LOGIN_MESSAGE_FRAGMENT = "LoginMessageFragment";
//    public static final String SET_SECRET_FRAGMENT = SetSecretFragment.class.getSimpleName();
    public static final String SET_SECRET_FRAGMENT = "SetSecretFragment";
    public static final String RESET_SECRET_FRAGMENT = "ResetSecretFragment";

    private FragmentLoginBinding binding;
    private FragmentPagerAdapter adapter;
    private LoginViewModel viewModel;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        viewModel = new ViewModelProvider(requireActivity(), new ViewModelFactory()).get(LoginViewModel.class);
        init();
        addListener(listener);
    }

    @Override
    public void initFragmentPages() {
        addPage(new LoginSecretFragment());
        addPage(new LoginMessageFragment());
        addPage(new SetSecretFragment());
        addPage(new ResetSecretFragment());
    }

    @Nullable
    @Override
    public View onCreateView(@NotNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = FragmentLoginBinding.inflate(inflater);
        binding.viewpager.setAdapter(adapter);
        return binding.container;
    }

    private ActionListener listener = new ActionListener() {
        @Override
        public boolean onAction(ActionEnum action) {
            Log.e(TAG, "onAction="+ action);
            return false;
        }

        @Override
        public boolean onPageChanged(String pageName) {
            Log.e(TAG, "onPageChanged="+ pageName);
            switch (pageName){
                case LOGIN_SECRET_FRAGMENT:
                    binding.viewpager.setCurrentItem(0);
                    return true;
                case LOGIN_MESSAGE_FRAGMENT:
                    binding.viewpager.setCurrentItem(1);
                    return true;
                case SET_SECRET_FRAGMENT:
                    binding.viewpager.setCurrentItem(2);
                    return true;
                case RESET_SECRET_FRAGMENT:
                    binding.viewpager.setCurrentItem(3);
                    return true;
                default:
                    return false;
            }
        }
    };

    private void init(){
        adapter = new FragmentPagerAdapter(getChildFragmentManager(), FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
            @Override
            public int getCount() {
                return getPages().size();
            }

            @NonNull
            @Override
            public Fragment getItem(int position) {
                return getPage(position);
            }
        };
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        removeListener(listener);
    }

    private void showLoginFailed(String errorString) {
        if (getContext() != null && getContext().getApplicationContext() != null) {
            Toast.makeText(
                    getContext().getApplicationContext(),
                    errorString,
                    Toast.LENGTH_LONG).show();
        }
    }
}
